package com.xxxx.service.impl;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.AdminUtils;
import com.xxxx.mapper.MenuMapper;
import com.xxxx.pojo.Admin;
import com.xxxx.pojo.Menu;
import com.xxxx.pojo.R;
import com.xxxx.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chao
 * @since 2022-09-26
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 根据用户Id查询菜单列表
     * @return
     */

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public List<Menu> getMenuByAdminId() {
        Admin admin= AdminUtils.getCurrentAdmin();
        Integer adminId=admin.getId();
        String menuJsonStr = stringRedisTemplate.opsForValue().get("menu:" + adminId);
        if (menuJsonStr == null) {
            List<Menu> menus = menuMapper.getMenusByAdminId(adminId);
            String jsonString = JSONUtil.toJsonStr(menus);
            stringRedisTemplate.opsForValue().set("menu:" + adminId, jsonString);
            return menus;
        }
        List<Menu> menus = JSONUtil.toList(menuJsonStr, Menu.class);
        return menus;

    }

    @Override
    public List<Menu> getMenusWithRole() {

       return menuMapper.getMenusWithRole();
    }

    @Override
    public List<Menu> getAllMenus() {
        return menuMapper.getAllMenus();
    }


}
